const path = require('path');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const bodyParser = require('body-parser');
const express = require('express');
const common = require("./utils/common")
//引入各种router对象
const indexRouter = require('./routes')
const indexRouters = require('./routes/passport')
const detailRouter = require('./routes/detail')
const profileRouter = require('./routes/profile')


const appConfig = app =>{
    app.use(express.static('public'))
    app.engine('html',require('express-art-template'));
    app.set('view options',{
        debug:process.env.NODE_ENV !== 'development'
    });
    app.set('views',path.join(__dirname,'views'));
    app.set('view engine','html');
    
    // 获取post请求参数的配置
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(bodyParser.json());
    
    // 注册cookie和session
    app.use(cookieParser());
    app.use(cookieSession({
        name:'my_session',
        keys:['djak1902ek0192dk0dk91dk10odk'],
        maxAge:1000*60*60*24*2
    }))  
    app.use(common.csrfProtect,indexRouter) 
    app.use(common.csrfProtect,indexRouters)
    app.use(common.csrfProtect,detailRouter)
    app.use(common.csrfProtect,profileRouter)
    app.use((req,res)=>{
        (async function(){
            const userInfo = await common.getUser(req,res)
            const data = {
                user_info:userInfo[0]?{
                    nick_name:userInfo[0].nick_name,
                    avatar_url:userInfo[0].avatar_url
                }:false,
            }
           res.render('news/404',data)
        })()
    })
}
module.exports = appConfig